<?php
/**
 * Model for a PHP Codesnigger log.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Model for a PHP Codesnigger log.
 *
 * @package PhpCilux
 */
class Model_Log_PhpCs extends Model_Log_Abstract
{

    /**
     * Constructor.
     *
     * @param string $projectKey The project key.
     * @param string $buildKey   The build key.
     */
    public function __construct($projectKey, $buildKey)
    {
        parent::__construct($projectKey, $buildKey);

        $this->_logFilename = 'phpcs.txt';
    }

    /**
     * Get the status of the PHP Codesniffer log.
     *
     * @return string
     */
    public function status()
    {
        $status = 'success';

        $log   = $this->getContent();
        $lines = explode("\n", $log);

        while (list(, $line) = each($lines)) {
            $line = rtrim($line);

            if (false !== stripos($line, '| ERROR |') || false !== stripos($line, '| ERROR   |')) {
                return 'failed';
            } elseif (false !== stripos($line, '| WARNING |')) {
                $status = 'warning';
            }
        }

        return $status;
    }

    /**
     * Formating the log.
     *
     * @param string $content The log content which will be styled.
     *
     * @return string
     */
    public function format($content)
    {
        $lines   = explode("\n", $content);
        $content = '<h4>PHP_CodeSniffer</h4>';

        $errors   = 0;
        $warnings = 0;
        $last     = '';

        while (list($key, $line) = each($lines)) {
            $line = rtrim($line);

            switch(true) {
                case false !== stripos($line, '| ERROR '):
                    $errors++;

                case false !== stripos($line, '|       ') && 'failed' == $last:
                    $content .= '<div class="failed"><pre>' . $line . '</pre></div>';
                    $last     = 'failed';
                    break;

                case false !== stripos($line, '| WARNING '):
                    $warnings++;

                case false !== stripos($line, '|       ') && 'warning' == $last:
                    $content .= '<div class="warning"><pre>' . $line . '</pre></div>';
                    $last     = 'warning';
                    break;

                case 'FILE' == substr($line, 0, 4) && $key > 1:
                    $content .= '<br /><pre>' . $line . '</pre>';
                    $last     = '';
                    break;

                case false !== stripos($line, 'ERROR(S) AFFECTING'):
                case '' == trim(trim($line, '-')):
                    break;

                default:
                    $content .= '<pre>' . $line . '</pre>';

                    $last = '';
                    break;
            }
        }

        $content .= '<br />';
        $content .= $this->_formatSummary($errors, $warnings);
        $content .= '<br />';

        return $content;
    }

    /**
     * Create the summary line.
     *
     * @param integer $errors   The errors count.
     * @param integer $warnings The warnings count.
     *
     * @return $string;
     */
    private function _formatSummary($errors = 0, $warnings = 0)
    {
        switch(true) {
            case ($errors > 0 && $warnings > 0):
                $class   = 'failed';
                $summary = 'Found ' . $errors . ' error(s) and ' . $warnings . ' warning(s)';
                break;

            case ($errors > 0 && $warnings == 0):
                $class   = 'failed';
                $summary = 'Found ' . $errors . ' error(s)';
                break;

            case ($errors == 0 && $warnings > 0):
                $class   = 'warning';
                $summary = 'Found ' . $warnings . ' warning(s)';
                break;

            default:
                $class   = 'success';
                $summary = 'OK';
                break;
        }

        $content .= "\n" . '<div class="' . $class . '"><pre>';
        $content .= $summary;
        $content .= "</pre></div>\n";

        return $content;
    }

}